#ifndef EDITORWIDGET_H
#define EDITORWIDGET_H

#include <QWidget>
#include <QTextDocument>
#include <QScrollArea>
#include <QTextEdit>
#include <QPrinter>
#include <QFont>
#include <QWheelEvent> 
#include <QScrollBar>
#include <QAbstractTextDocumentLayout>
#include <QTextImageFormat> 
#include <QDateTime>  
#include <QPainterPath>
class EditorWidget : public QWidget
{
    Q_OBJECT
public:
    explicit EditorWidget(QWidget* parent = nullptr);

	void setPageSizeA4();
	void addPage();
	int pageCount() const;
    void setPageMargins(int left, int top, int right, int bottom); 

	void zoomIn();
	void zoomOut();
	void resetZoom();
	qreal currentZoom() const;

	void undo();
	void redo();
	void copy();
	void paste();
	void cut();
	bool hasSelection() const;

	void applyBold(bool bold);
	void applyItalic(bool italic);
	void applyUnderline(bool underline);
	void applyStrikethrough(bool strikethrough);
	void applyFontFamily(const QString& family);
	void applyFontSize(int size);
	void applyHeading1();
	void clearFormat();
    void applyFontColor(const QColor& color); 
    void applyHighlightColor(const QColor& color);
    void applyParagraphAlignment(Qt::Alignment alignment);
    void resetToDefaultStyle();
    void setStyleMarker(const QString& styleName);
    void clearStyleMarker();
    QColor currentHighlightColor() const;
    QString getSelectedStyleMarker();
 
    void setDocumentContent(const QString& html); 
    void setPlainText(const QString& text); 
    void setHtml(const QString& html);     
    QString getDocumentContent() const;         
    QString toPlainText() const { return m_textEdit->toPlainText(); }
    QString toRtf() const { return m_textEdit->toHtml(); };            
    QTextDocument* document() const { return m_textEdit->document(); }

	void setModified(bool modified);
	bool isModified() const;
	void markAsSaved();
	QDateTime lastSavedTime() const;
    void onTextChanged();
signals:
    void formatChanged
    (
        bool isBold,
        bool isItalic,
        bool isUnderline,
        bool isStrikethrough,
        const QString& fontFamily,
        int fontSize,
        const QColor& fontcolor,
        const QColor& highlightcolor,
        Qt::Alignment alignment,
        const QString& styleMarker
    );

    void pageCountChanged(int count);
    void contentsChanged();
    void modificationStateChanged(bool isModified); 
public slots:
    void insertImage(const QString& imagePath);
    void insertTable(int rows, int columns);
	void onShapeSelected(const QString& shapeType);
    void setCurrentShape(const QString& shapeType);

protected:
	void mousePressEvent(QMouseEvent* event) override;
	void mouseReleaseEvent(QMouseEvent* event) override;
	void paintEvent(QPaintEvent* event) override;

private slots:
  
    void onTextFormatChanged();
    void onTextContentChanged(); 

private:
	void setupA4Canvas();
	void updateZoom();
	QSize a4SizeToPixels() const;
	void updatePageLayout();
	Qt::Alignment currentParagraphAlignment() const;
	void wheelEvent(QWheelEvent* event);
	
	QScrollArea* m_scrollArea;
	QWidget* m_editorContainer;
	QTextEdit* m_textEdit;
	QTextDocument* m_document;
	QPrinter m_printer;
	qreal m_zoomFactor = 1.5;
	qreal m_baseFontSize = 12;
    QMargins m_pageMargins = QMargins(50, 50, 50, 50);       
	QSize m_pageSize;
	bool m_isModified = false;
	QDateTime m_lastSavedTime;
	QString m_currentShape;
	QList<QPainterPath> m_shapes;
	QPoint m_startPos;
	QPainterPath m_tempPath;
	bool m_isDrawing;
};

#endif // EDITORWIDGET_H